package com.kikt.recyclerview.loadmore

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup

/**
 * Created by cjl on 2017/10/26.
 */
class LoadMoreRecyclerView : RecyclerView {

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        initAttrs(attrs)
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context) : this(context, null)

    private fun initAttrs(attrs: AttributeSet?) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.LoadMoreRecyclerView)
        a.recycle()
    }

    private fun initView() {

    }

    private val headerViews = ArrayList<View>()
    private val footerViews = ArrayList<View>()

    fun addHeaderView(headerView: View) {
        headerViews.add(headerView)
    }

    fun removeHeaderView(headerView: View) {
        headerViews.remove(headerView)
    }

    fun addFooterView(footerView: View) {
        footerViews.add(footerView)
    }

    fun removeFooterView(footerView: View) {
        footerViews.remove(footerView)
    }

    override fun setAdapter(adapter: Adapter<*>?) {
        super.setAdapter(adapter)
    }

    companion object {
        val TYPE_HEADER_START = 100000
        val TYPE_FOOTER_START = 200000
        val TYPE_LOADMORE = 300000
    }

    /**
     * @param loadMoreView must impl the LoadMoreAble
     * @see LoadMoreAble
     */
    var loadMoreView: View? = null
        set(value) {
            field = value
            if (value !is LoadMoreAble) {
                throw RuntimeException("loadMoreView must impl the LoadMoreAble")
            }
        }

    private fun execLoadMore(loadmoreRunnable: (LoadMoreAble) -> Unit) {
        val loadMoreView = this.loadMoreView

        if (loadMoreView != null && loadMoreView is LoadMoreAble) {
            loadmoreRunnable.invoke(loadMoreView)
        }
    }


    private fun hasLoadMore(): Boolean {
        return false
    }

    inner class WarpAdapter<AdapterVH : ViewHolder>(private val adapter: Adapter<AdapterVH>) : RecyclerView.Adapter<ViewHolder>() {

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            when (getItemViewType(position)) {
                TYPE_FOOTER_START, TYPE_LOADMORE, TYPE_HEADER_START -> {
                }
                else -> {
                    adapter.onBindViewHolder((holder as AdapterVH), position - headerViews.count())
                }
            }
        }

        override fun getItemCount(): Int {
            return headerViews.count() + footerViews.count() + adapter.itemCount + if (hasLoadMore()) 1 else 0
        }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
            return when (viewType) {
                TYPE_FOOTER_START, TYPE_HEADER_START -> TODO() //createFooterHolder()
                TYPE_LOADMORE -> TODO() // EmptyViewHolder(setLoadMoreView())
                else -> adapter.onCreateViewHolder(parent, viewType)
            }
        }

        fun notifyData() {
            notifyDataSetChanged()
        }

        private val typeArray = SparseArray<Int>()

        override fun getItemViewType(position: Int): Int {
            //ps : header 为2 footer 为3 loadmore为1  adapter.count = 5
            //header: 0~1
            //normal: 2~6
            //footer: 7~8
            //loadmore:9
            //总数量为0

            val headerCount = headerViews.count()

            if (position < headerCount) {
                val type = TYPE_HEADER_START + position
                typeArray.append(type, position)
                return type
            }

            if (position in headerCount until headerCount + adapter.itemCount) {
                val type = adapter.getItemViewType(position - headerCount)
                typeArray.append(type, position)
                return type
            }

            if (hasLoadMore() && position == itemCount - 1) {
                return TYPE_LOADMORE
            } else {
                return TYPE_FOOTER_START + position
            }
        }

//        private val headerHolderList = ArrayList<EmptyViewHolder>()
//        private val footerHolderList = ArrayList<EmptyViewHolder>()

//        private fun createFooterHolder(itemView: View): EmptyViewHolder {
//            return EmptyViewHolder(itemView)
//        }

    }


    inner class EmptyViewHolder(itemView: View?) : ViewHolder(itemView) {

    }
}

interface LoadMoreAble {

}